//---------------------------------------------------------------------
// File: ApplicationStatusStep.cs
// 
// Summary: 
using System;
using System.Xml;
using Microsoft.BizTalk.ExplorerOM;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions.BizTalk
{
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    /// <TestStep assemblyPath="BizUnitExtensions.dll" typeName="BizUnit.Extensions.ApplicationStatusStep">
    ///		<ApplicationName></ApplicationName>
    ///		<ConnectionString></ConnectionString>
    ///		<Status>NotApplicable|PartiallyStarted|Started|Stopped</Action>
    /// </TestStep>
    /// </code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>DelayForCompletion</term>
    ///			<description>The number of seconds to deplay for this step to complete</description>
    ///		</item>
    ///		<item>
    ///			<term>ApplicationName</term>
    ///			<description>The name of the Application to containing the Application to start/stop</description>
    ///		</item>
    /// 	<item>
    ///			<term>ConnectionString</term>
    ///			<description>The connectionstring to the management database</description>
    ///		</item>
    ///		<item>
    ///			<term>Status</term>
    ///			<description>NotApplicable|PartiallyStarted|Started|Stopped</description>
    ///		</item>
    ///	</list>
    ///	</remarks>
	
	public class ApplicationStatusStep : ITestStep
	{
		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			string applicationName = context.ReadConfigAsString(testConfig, "ApplicationName");
            string connectionString = context.ReadConfigAsString(testConfig, "ConnectionString");
            Status status = (Status)Enum.Parse(typeof(Status), context.ReadConfigAsString(testConfig, "Status"), true);
		    
		    //check status of Application
		    Status actualStatus = CheckApplicationStatus(applicationName, context, connectionString);

            if (status != actualStatus)
			{
				throw new ApplicationException(string.Format("The application: {0} was found to not be in the state Stopped = {1}", applicationName, actualStatus.ToString()));
			}
			else
			{
                context.LogInfo(string.Format("The application: {0} was found to be in the state Stopped = {1}", applicationName, actualStatus.ToString()));
			}
		}

        private Status CheckApplicationStatus(string applicationName, Context context, string connectionString)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();
            root.ConnectionString = connectionString;
            foreach (Application application in root.Applications)
            {
                if (application.Name == applicationName)
                {
                    context.LogInfo("Checking Application {0} status ", applicationName);
                    return application.Status;
                }
            }
            return Status.NotApplicable;
        }
	}
}
